package com.lq.createPattern.singleton.type2;

public class SingletonTest2 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        Singleton singleton1 = Singleton.getInstance();
        System.out.println(singleton1 == singleton);
        System.out.println(singleton1.hashCode());
        System.out.println(singleton.hashCode());
    }
}

//饿汉式 静态代码块
class Singleton {
    //1.构造器私有化，防止外部能new
    private Singleton() {
    }

    //2.本类内部类创建对象实例
    private static Singleton instance;

    static {
        instance = new Singleton();
    }

    //3.提供一个公有的静态方法，返回对象实例
    public static Singleton getInstance() {
        return instance;
    }
}
